/**
 * 根据图层信息清楚地图图层(单个)
 * @param mapboxInstance
 * @param layer layer Object / layer id
 */
export function clearMapLayerByLayer(mapboxInstance: any, layer: any) {
  const id: number = typeof layer === 'number' ? layer : layer.id
  const mapLayerLabelId: string = `label-${id}`
  const mapLayerId: string = `${id}`
  const mapLayerSourceId: string = `source-${id}`
  const mapArrowLayerId: string = `arrow-layer-${id}`
  const mapArrowLayerSourceId: string = `arrow-source-${id}`
  // 图层 label
  const checkLayerLabelIsExist: boolean = !!mapboxInstance.getLayer(
    mapLayerLabelId
  )
  if (checkLayerLabelIsExist) {
    mapboxInstance.removeLayer(mapLayerLabelId)
  }
  // 图层
  const checkLayerIsExist: boolean = !!mapboxInstance.getLayer(mapLayerId)
  if (checkLayerIsExist) {
    mapboxInstance.removeLayer(mapLayerId)
    mapboxInstance.removeSource(mapLayerSourceId)
  }
  // 有箭头移除箭头
  const checkLayerArrowIsExist: boolean = !!mapboxInstance.getLayer(
    mapArrowLayerId
  )
  if (checkLayerArrowIsExist) {
    mapboxInstance.removeLayer(mapArrowLayerId)
    mapboxInstance.removeSource(mapArrowLayerSourceId)
  }
}

/**
 * 根据图层信息清楚地图图层(多个)
 * @param mapboxInstance
 * @param layers  可为 layer Object [] / layer id []
 */
export function clearMapLayersByLayers(mapboxInstance: any, layers: any[]) {
  layers.forEach((layer: any) => {
    clearMapLayerByLayer(mapboxInstance, layer)
  })
}
